package com.ly.cloud.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.ly.cloud.common.mybatisplus.plugins.PageInfo;
import com.ly.cloud.dto.DepartmentDto;
import com.ly.cloud.service.DepartmentService;
import com.ly.cloud.vo.DepartmentVo;
import com.ly.cloud.vo.DeptTypeSelectVo;
import com.ly.cloud.vo.TeachDepVo;
import com.ly.cloud.web.utils.WebResponse;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @Description: 部门(单位)Controller
 * @Author: pengshiqng
 * @Date: Create in 2018/12/12
 */
@RestController
@RequestMapping("/department")
@Api("部门(单位)Controller")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService ;

    /**
     * 分页+条件查询
     */
    @RequestMapping(value = "/queryPageList", method = RequestMethod.POST)
    public WebResponse<PageInfo<DepartmentVo>> queryPageList(@RequestParam String id , @RequestParam int pageNum, @RequestParam int pageSize, @RequestBody DepartmentDto dto){
        Page<DepartmentVo> page = departmentService.selectPage(new Page<DepartmentVo>(pageNum, pageSize), id, dto);
        return new WebResponse<PageInfo<DepartmentVo>>().success(PageInfo.toPageInfo(page));
    }


    /**
     * 根据id删除
     */
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.POST)
    public WebResponse<Integer> deleteById(@PathVariable String id){
        int result = departmentService.deleteById(id) ;
        return new WebResponse<Integer>().success(result);
    }

    /**
     * 根据id批量删除
     */
    @RequestMapping(value = "/deleteByIds", method = RequestMethod.POST)
    public WebResponse<Integer> deleteByIds(@RequestBody List<String> ids){
        int result = departmentService.deleteByIds(ids) ;
        return new WebResponse<Integer>().success(result);
    }

    /**
     * 插入
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public WebResponse<Boolean> insert(@RequestHeader(required=false,value = "loginUserName") String loginUserName,@RequestBody DepartmentDto dto){
        departmentService.insert(loginUserName ,dto);
        return new WebResponse<Boolean>().success(true) ;
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public WebResponse<Boolean> update(@RequestHeader(required=false,value = "loginUserName") String loginUserName,@RequestBody DepartmentDto dto){
        departmentService.update(loginUserName , dto);
        return new WebResponse<Boolean>().success(true) ;
    }

    /**
     * 查询所有部门名称  树形结构
     * 如果传参数parentDwh，这查询改部门下的子部门，否则，查询最顶级的部门
     */
    @RequestMapping(value = "/findAllDep", method = RequestMethod.GET)
    public WebResponse<List<TeachDepVo>> findAllDepartment(@RequestParam(required = false, value = "parentDwh") String parentDwh){
        List<TeachDepVo> list = departmentService.findAllDepartment(parentDwh);
        return new WebResponse<List<TeachDepVo>>().success(list) ;
    }
    
    /**
     * 查询教学机构（院系下拉）
     * 教学机构代码值固定：01，参考代码集的单位类别码
     * @author caihuihai 2018-12-20
     */
    @RequestMapping(value = "/findTeachDep", method = RequestMethod.GET)
    public WebResponse<List<TeachDepVo>> findTeachDep(){
        List<TeachDepVo> list = departmentService.findTeachDep();
        return new WebResponse<List<TeachDepVo>>().success(list) ;
    }


    /**
    * @Description: 查询所有单位类别(下拉通用)
    * @Author: pengshiqing
    * @CreateDate: 2018/12/22
    */
    @RequestMapping(value = "/findDepType", method = RequestMethod.GET)
    public WebResponse<List<DeptTypeSelectVo >> findDepType(){
        List<DeptTypeSelectVo > list = departmentService.findDepType();
        return new WebResponse<List<DeptTypeSelectVo >>().success(list) ;
    }

    /**
    * @Description: 新增部门时，查询部门编号是否存在
    * @Author: pengshiqing
    * @CreateDate: 2018/12/22
    */
    @RequestMapping(value = "/checkedDepID", method = RequestMethod.GET)
    public WebResponse<Boolean> checkedDepID(@RequestParam("depId") String depId){
        Boolean b = departmentService.checkedDepID(depId);
        return new WebResponse<Boolean>().success(b) ;
    }






    
}
